package com.naodong.shenluntiku.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.jakewharton.rxbinding2.view.RxView;
import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.di.component.DaggerSafeVerComponent;
import com.naodong.shenluntiku.di.module.SafeVerModule;
import com.naodong.shenluntiku.mvp.contract.SafeVerContract;
import com.naodong.shenluntiku.mvp.model.bean.IdAndCon;
import com.naodong.shenluntiku.mvp.model.bean.ResponseMsg;
import com.naodong.shenluntiku.mvp.presenter.SafeVerPresenter;
import com.naodong.shenluntiku.mvp.view.adapter.IdAndConAdapter;
import com.yatatsu.autobundle.AutoBundleField;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import me.shingohu.man.base.BaseMVPActivity;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.util.ToastUtil;
import me.shingohu.man.util.UiUtils;
import me.shingohu.man.util.ViewUtil;

/**
 * Created by hujie on 17/5/25.
 * 设置或者验证安全验证
 */

public class SetOrCheckSafeVerActivity extends BaseMVPActivity<SafeVerPresenter> implements SafeVerContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.answerET)
    EditText answerET;

    @BindView(R.id.questionTV)
    TextView questionTV;


    List<IdAndCon> questions;

    IdAndCon selectIdAndCon;

    @BindView(R.id.saveBtn)
    TextView saveTV;


    IdAndConAdapter mAdapter;

    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;

    @AutoBundleField
    boolean isCheckSafeVer;//是否是验证还是设置

    @Override
    protected void onCreateInit(Bundle savedInstanceState) {
        setStatusColor(R.color.color5DB0FF);
        KeyboardUtils.hideSoftInput(this);

        if (isCheckSafeVer) {
            saveTV.setText("确定");
            tv1.setText("此账号已设置安全验证");
            tv2.setText("请输入正确的验证问题和答案");
        }

        RxView.touches(mRootView, event -> {
            KeyboardUtils.hideSoftInput(answerET);
            return false;
        }).subscribe();

        RxView.focusChanges(answerET).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(@NonNull Boolean aBoolean) throws Exception {
                if (!aBoolean) {
                    KeyboardUtils.hideSoftInput(answerET);
                }
            }
        });
    }


    @Override
    public boolean supportActionbar() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSafeVerComponent.builder().appComponent(appComponent).safeVerModule(new SafeVerModule(this)).build().inject(this);
    }

    @OnClick(R.id.questionView)
    public void onQuestionViewClick() {
        if (questions == null) {
            mPresenter.requestSafeVerQuestions();
        } else {
            showRecycleView();
        }
    }

    @OnClick(R.id.saveBtn)
    public void onSaveClick() {

        if (!ViewUtil.isTextEmpty(questionTV)) {
            if (selectIdAndCon == null) {
                return;
            }
            if (!isCheckSafeVer) {
                //设置安全问题
                AlertDialog dialog = new AlertDialog.Builder(this).setTitle("提示")
                        .setMessage("请务必牢记验证问题和答案,否则将无法登陆")
                        .setNegativeButton("再看看", (dialog1, which) -> {
                            dialog1.dismiss();
                        })
                        .setPositiveButton("记住了", (dialog1, which) -> {
                            dialog1.dismiss();
                            mPresenter.requestSetSafeVerQuestion(selectIdAndCon.getId(), ViewUtil.getText(answerET));
                        })
                        .setCancelable(false)
                        .create();
                dialog.show();
            } else {
                mPresenter.requestCheckSafeVerQuestion(selectIdAndCon.getId(), ViewUtil.getText(answerET));
            }
        }

    }

    @Override
    protected boolean userAutoBindBundle() {
        return true;
    }

    @Override
    protected int layoutId() {
        return R.layout.a_setsafever;
    }

    @Override
    public void showMessage(String msg) {

    }

    private void initRecyclerView() {
        mAdapter = new IdAndConAdapter(questions);
        UiUtils.configRecycleView(recyclerView, new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            IdAndCon idAndCon = (IdAndCon) adapter.getItem(position);
            recyclerView.setVisibility(View.INVISIBLE);
            answerET.setVisibility(View.VISIBLE);
            questionTV.setText(idAndCon.getCon());
            if (selectIdAndCon != idAndCon) {
                answerET.setText("");
            }
            this.selectIdAndCon = idAndCon;
        });
    }

    @Override
    public void onRequesrStart() {
        progressHudUtil.showProgressHud();
    }

    @Override
    public void onRequestFinish() {
        progressHudUtil.dismissProgressHud();
    }

    @Override
    public void onSafeVerQuestions(List<IdAndCon> questions) {
        this.questions = questions;
        showRecycleView();
    }

    @Override
    public void onSetSafeVerQuestion(ResponseMsg msg) {
        ToastUtil.makeToast(msg.getMsg());

        Intent intent = new Intent(mActivity, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onCheckSafeVerQuestion(ResponseMsg msg) {
        ToastUtil.makeToast(msg.getMsg());
        Intent intent = new Intent(mActivity, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onTrustClient(ResponseMsg msg) {

    }

    private void showRecycleView() {
        if (mAdapter == null) {
            initRecyclerView();
        }
        recyclerView.setVisibility(View.VISIBLE);
        answerET.setVisibility(View.INVISIBLE);
    }


}
